from django.shortcuts import render, redirect, get_object_or_404, get_list_or_404
from django.http import HttpResponse
from time import time
from django.template import loader
from .models import ClassRoom
from .forms import TestForm, ClassRoomCreateForm
# csrf的免疫防护
from django.views.decorators.csrf import csrf_exempt

from django.views.generic import TemplateView, ListView, DetailView, FormView, CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy, reverse


# Create your views here.
# v
def test1(request):
    return HttpResponse('hello world 111')


# TV 模式
def test2(request):
    context = {
        'title': time()
    }
    return render(request, 'test1/test2.html', context)


# MTV 模式
def test3(request):
    # ClassRoom.objects.all()
    # select * from test1_classroom

    # ClassRoom.objects.all()[0]
    # select * from test1_classroom where id = 1

    class_room = ClassRoom.objects.all()[0]
    context = {
        'data': class_room
    }
    return render(request, 'test1/test3.html', context)


# 最原始的渲染模板方式
def test4(request):
    class_room = ClassRoom.objects.all()[0]
    tmp = loader.get_template('test1/test4.html')
    context = {
        'data': class_room
    }
    return HttpResponse(tmp.render(context, request))


# 最原始的渲染模板方式
def test5(request):
    tmp = loader.get_template('test1/test5.html')
    context = {

    }
    return HttpResponse(tmp.render(context, request))


# 测试js css静态文件
def test6(request):
    context = dict()
    return render(request, 'test1/test6.html', context)


def test7(request, class_id):
    data = ClassRoom.objects.filter(id=class_id)[0]
    context = {
        'data': data
    }
    return render(request, 'test1/test7.html', context)


# 表单处理函数
def test8(request):
    test_form = TestForm()
    content = {
        'form': test_form
    }
    return render(request, 'test1/test8.html', content)


# csrf的防护
@csrf_exempt
def test9(request):
    content = dict()
    return render(request, 'test1/test9.html', content)


# 自定义get post的函数请求
@csrf_exempt
def test10(request):
    methond = request.method

    if methond == 'GET':
        test_form = TestForm()
        content = {
            'form': test_form
        }
        return render(request, 'test1/test10.html', content)
    if methond == 'POST':

        data = request.POST

        vali_data = TestForm(data)
        if vali_data.is_valid():
            clean_data = vali_data.cleaned_data
            return redirect('test7', class_id='2')
        return redirect('test4')


# view
def test11(request, class_id):
    data = get_object_or_404(ClassRoom, pk=class_id)
    context = {
        'data': data
    }
    return render(request, 'test1/test7.html', context)


# list
def test12(request):
    data = get_list_or_404(ClassRoom)

    context = {
        'datas': data
    }
    return render(request, 'test1/test12.html', context)


# 模板类
class Test13(TemplateView):
    template_name = "test1/test2.html"


# list类
class Test14(ListView):
    model = ClassRoom
    template_name = 'test1/test14.html'
    context_object_name = 'datas'
    # queryset = ClassRoom.objects.all().order_by('-id')

    # def get_queryset(self):
    #     name = self.request.GET.get('name','')
    #     return ClassRoom.objects.filter(class_room_name__contains=name)


# detailview类
class Test15(DetailView):
    model = ClassRoom
    template_name = 'test1/test7.html'
    context_object_name = 'data'


# formview类 使用form类完成test10里面的内容
class Test16(FormView):
    template_name = 'test1/test16.html'
    form_class = TestForm
    success_url = reverse_lazy('test16')

    def form_valid(self, form):
        return super().form_valid(form)


# 使用form实现一个创建model的功能
from django.views.generic.detail import SingleObjectMixin
class Test17(FormView):
    template_name = 'test1/test17.html'
    form_class = ClassRoomCreateForm
    success_url = reverse_lazy('test17')

    def form_valid(self, form):
        data = form.cleaned_data
        self.object = ClassRoom.objects.create(**data)

        return super().form_valid(form)

    def get_success_url(self):
        return reverse('test15', kwargs={'pk': self.object.pk})


# 使用createview来实现
class Test18(CreateView):
    template_name = 'test1/test18.html'
    model = ClassRoom
    fields = ['class_id', 'class_person_num', 'class_room_name', 'class_computer_num']


# 使用UpdateView来实现
class Test19(UpdateView):
    template_name = 'test1/test19.html'
    model = ClassRoom
    fields = ['class_person_num']


# DeleteView
class Test20(DeleteView):
    template_name = 'test1/test20.html'
    model = ClassRoom
    success_url = reverse_lazy('test9')
